package com.youmingxing.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.List;

/**
 * 字符串工具类.
 */
public class StringUtil {

    /**
     * @description 验证字符串是否为空
     * @version 1.0
     */
    public static boolean isEmpty(String str) {
        return str == null || str.equals("null") || str.trim().equals("");

    }

    public static boolean num(Object o) {
        int n = 0;
        try {
            n = Integer.parseInt(o.toString().trim());
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return n > 0;
    }

    public static boolean decimal(Object o) {
        double n = 0;
        try {
            n = Double.parseDouble(o.toString().trim());
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return n > 0.0;
    }


    public static String readStream(InputStream is) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;

        StringBuilder out = new StringBuilder();
        byte[] b = new byte[4096];
        for (int n; (n = is.read(b)) != -1; ) {
            out.append(new String(b, 0, n));
        }
        return out.toString();
    }

    /**
     * 将字符串转换成浏览器utf-8的形式
     *
     * @param s
     * @return
     */
    public static String getUtf8Url(String s) {
        try {
            s = URLEncoder.encode(s, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return s;
    }


    /**
     * 判断是否是一个中文汉字
     *
     * @param c 字符
     * @return true表示是中文汉字，false表示是英文字母
     * @throws UnsupportedEncodingException 使用了JAVA不支持的编码格式
     */
    public static boolean isChineseChar(char c){
        // 如果字节数大于1，是汉字
        // 以这种方式区别英文字母和中文汉字并不是十分严谨，但在这个题目中，这样判断已经足够了
        try {
            return String.valueOf(c).getBytes("UTF-8").length > 1;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * stringToInt
     *
     * @param text
     * @return
     */
    public static int stringToInt(String text) {
        int number = 0;
        try {
            number = Integer.parseInt(text);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return number;
    }

    public static float stringToFloat(String text) {
        float number = 0f;
        try {
            number = Float.parseFloat(text);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return number;
    }

    /**
     * list转string
     * @param list
     * @return
     */
    public static String listToString(List<String> list) {
        int length = list.size() ;
        StringBuilder buf = new StringBuilder("");
        if(length > 0){
            buf.append(list.get(0));
        }
        for(int i = 1 ; i < length ; i++){
            buf.append(',');
            buf.append(list.get(i));
        }
        return buf.toString();
    }

    public static String generateMD5(String inStr) {
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuilder hexValue = new StringBuilder();
        for (byte md5Byte : md5Bytes) {
            int val = ((int) md5Byte) & 0xff;
            if (val < 16)
                hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString().toUpperCase();

    }
}
